<template>
  <!-- 维修站维护页面 -->
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.repairsit_delete" @click="handleDelete">删 除 </el-button>
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
      <template slot-scope="{ row }" slot="status">
        <el-tag>{{ row.status === 0 ? '否' : '是' }}</el-tag>
      </template>
      <template slot="principalNameForm">
        <el-input placeholder="请选择负责人" v-model="form.principalName" class="input-with-select" disabled>
          <el-button
            slot="append"
            icon="el-icon-circle-close"
            v-if="form.principalName"
            @click="
              form.principalName = '';
              form.principal = '';
            "
          ></el-button>
          <el-button slot="append" icon="el-icon-search" @click="openWorkOrderNoModal()"></el-button>
        </el-input>
      </template>
    </avue-crud>
    <common-modal-template ref="commonModalTemplate" :option="tableOption" @getSelectedRows="getSelectedRows"> </common-modal-template>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from '@/api/repair/repairSite';
import { mapGetters } from 'vuex';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';

export default {
  components: {
    CommonModalTemplate,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'REPAIRSITE',
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      selectData: [],
      code: '00',
      searchLabelWidth: 100,
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        viewTitle: '详情',
        viewBtnText: '详情',
        selection: true,
        dialogClickModal: false,
        searchLabelWidth: 120,
        columnBtn: false,
        column: [
          {
            label: '维修站名称',
            prop: 'name',
            labelWidth: 120,
            search: true,
            searchOrder: 2,
            order: 1,
            rules: [
              {
                required: true,
                message: '请输入内容',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '维修站编码',
            prop: 'code',
            labelWidth: 120,
            order: 2,
            rules: [
              {
                required: true,
                message: '请输入内容',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '所属车间',
            prop: 'workshopId',
            type: 'select',
            labelWidth: 120,
            search: true,
            searchOrder: 1,
            order: 3,
            // dicUrl: '/api/blade-system/dept/workshop-list',
            dicUrl: '/api/mes-product/productionline/query-workshop-line-list',
            props: {
              label: 'workshopName',
              value: 'workshopId'
            },
            rules: [
              {
                required: true,
                message: '请选择车间',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '负责人',
            prop: 'principalName',
            labelWidth: 120
          },
          {
            label: '负责人id',
            prop: 'principal',
            hide: true,
            display: false
          }
        ]
      },
      tableOption: {} //公用模态框配置信息
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.repairsit_add, false),
        viewBtn: this.vaildData(this.permission.repairsit_view, false),
        delBtn: this.vaildData(this.permission.repairsit_delete, false),
        editBtn: this.vaildData(this.permission.repairsit_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    /**
     * 选择模态框行数据
     */
    getSelectedRows(rowData) {
      if (this.modalName === 'workOrderModal') {
        this.form.principal = rowData.id;
        this.form.principalName = rowData.realName;
      }
    },
    /**
     * 打开工单号选择模态框
     */
    openWorkOrderNoModal() {
      this.modalName = 'workOrderModal';
      this.$refs.commonModalTemplate.showModal();
      let url = '/api/blade-system/user/page';
      this.tableOption = {
        modalTitle: '选择负责人',
        url: url,
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '登录账号',
            prop: 'account',
            search: true,
            display: false
          },
          {
            label: '所属租户',
            prop: 'tenantName',
            slot: true,
            display: false
          },
          {
            label: '用户姓名',
            prop: 'realName',
            search: true,
            display: false
          },
          {
            label: '所属角色',
            prop: 'roleName',
            slot: true,
            display: false
          },
          {
            label: '所属部门',
            prop: 'workshopName',
            slot: true,
            display: false
          },
          {
            label: '用户平台',
            prop: 'userTypeName',
            slot: true,
            display: false
          }
        ]
      };
    },
    rowSave(row, done, loading) {
      const obj = {
        code: row.code,
        workshopId: row.workshopId,
        name: row.name,
        principal: row.principal
      };
      add(obj).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    getSelectData(code) {
      getSelectList(code)
        .then((res) => {
          // debugger
          this.selectData = res.data.data;
          // console.log(res.data.data, 'resresas')
        })
        .catch((err) => {
          this.message(err);
        });
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        // [] || dataListTable
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style></style>
